import axios, {AxiosInstance, AxiosError, AxiosResponse, AxiosRequestConfig,AxiosRequestHeaders} from 'axios';
import { useRouter } from 'vue-router';
import { ElMessage } from 'element-plus';


const service:AxiosInstance = axios.create({
    timeout: 5000
});
service.interceptors.request.use(
    (config: AxiosRequestConfig) => {
        config.headers={
            ['fmToken']: String(localStorage.getItem('ms_token'))
        }

        return config;
    },
    (error: AxiosError) => {
        console.log(error);
        return Promise.reject();
    }
);

service.interceptors.response.use(
   
    (response: AxiosResponse) => {
        if (response.status === 200) {
            return response;
        }
         else {
            Promise.reject();
        }
    },
    (error: AxiosError) => {
        
         if (error.response?.status === 401){
            ElMessage.error("登录失效");
            localStorage.removeItem("ms_username")
            setTimeout(() => {
                window.open('/#/login','_self')
            }, 2000);
        }
        console.log(error);
        return Promise.reject();
    }
);

export default service;
